//
//  ITKeyboardUserSettingInputTextViewManager.m
//  ITBE
//
//  Created by ITBE on 2019/2/28.
//  Copyright © 2019 ITBE. All rights reserved.
//

#import "ITKeyboardUserSettingInputTextViewManager.h"
#import "ITUserSettingInputTextView.h"
@interface ITKeyboardUserSettingInputTextViewManager()<ITUserSettingInputTextViewProtocol>
{
    ITUserSettingInputTextView *keyboardTextField;
    UserSettingKeyboardTextFieldCallback currentCallback;
}
@end
@implementation ITKeyboardUserSettingInputTextViewManager
+(instancetype)shareManager
{
    static ITKeyboardUserSettingInputTextViewManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ITKeyboardUserSettingInputTextViewManager alloc] init];
    });
    return manager;
}

-(void)showKeyboardTextFieldWithText:(NSString *)text title:(NSString *)title placeHolder:(NSString *)placeHolder maxLength:(int)maxLength callback:(UserSettingKeyboardTextFieldCallback)callback
{
    currentCallback = callback;
    if (keyboardTextField == nil) {
        keyboardTextField = [[ITUserSettingInputTextView alloc] initWithTitle:title placeHolder:placeHolder maxLength:maxLength frame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 200)];
        keyboardTextField.delegate = self;
        [[UIApplication sharedApplication].keyWindow addSubview:keyboardTextField];
    }
    [keyboardTextField becomeFirstResponseWithText:text];
}

-(void)destroyKeyboardTextField
{
    if (keyboardTextField) {
        [keyboardTextField removeFromSuperview];
        keyboardTextField = nil;
    }
}

#pragma -mark ITUserSettingInputTextViewProtocol
-(void)resignFirstResponseWithText:(NSString *)text
{
    [self destroyKeyboardTextField];
    if (currentCallback) {
        currentCallback(text);
    }
}

-(void)didCancelAddComment
{
    [self destroyKeyboardTextField];
}
@end
